package com.corticon;

import org.springframework.web.context.ContextLoaderListener;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContext;
import java.io.*;
import java.util.Properties;

public class DialogsContextLoaderListener extends ContextLoaderListener {
    private static Log log = LogFactory.getLog(DialogsContextLoaderListener.class);

    @Override
    public void contextInitialized(ServletContextEvent event) {
		super.contextInitialized(event);
        ServletContext servletContext = event.getServletContext();
        String version = getVersion();
        servletContext.setAttribute("version", version);
	}

    private String getVersion() {
        String classPath = DialogsContextLoaderListener.class.getResource("/").getPath();
        InputStream ins = null;
        try {
            ins = new BufferedInputStream(new FileInputStream(classPath + "/version"));
            Properties prop = new Properties();
            prop.load(ins);
            return prop.getProperty("version");
        } catch (FileNotFoundException e) {
            log.error("File version cannot find");
        } catch (IOException e) {
            log.error("IOException");
        }
        return "default version";
    }
}
